
package services;

import dao.ProblemJpaController;
import dao.exceptions.NonexistentEntityException;
import entities.Problem;
import exceptions.DatabaseException;
import exceptions.NonExistentObjectException;
import exceptions.PreexistingObjectException;
import java.util.ArrayList;
import java.util.List;
import vo.VOProblem;

/**
 *
 * @author sbaldrich
 */
public class ProblemService {

    private static ProblemJpaController pc;
    public static ProblemJpaController getInstance()
    {
        if(pc == null)
            pc = new ProblemJpaController();
        return pc;
    }

    private static ContestService cs;
    public static ContestService getContestService()
    {
        if(cs == null)
            cs = new ContestService();
        return cs;
    }


    public void save(VOProblem vo) throws Exception
    {
        pc = getInstance();
        cs = getContestService();
        Problem p = findObject(vo.getContestCode());
        if(p!=null)
            throw new PreexistingObjectException("Problem Code");
        p = new Problem();
        try
        {
            p.setContest(cs.findObject(vo.getContestCode()));
            pc.create(p);
        } catch (Exception e)
        {
            if(e instanceof NonExistentObjectException)
                throw new NonExistentObjectException("Contest");
            throw new DatabaseException("ProblemService.save");
        }
    }

    public VOProblem find(String problemCode)throws Exception
    {
        Problem p = findObject(problemCode);
        if(p == null)
            return null;
        return p.getVO();
        
    }
    public Problem findObject(String problemCode)throws Exception
    {
        pc = getInstance();
        try
        {
            return pc.findProblem(problemCode);
        }
        catch (Exception e)
        {
            if(e instanceof DatabaseException)
                throw e;
            return null;
        }
    }

    public void update(VOProblem vo)throws Exception
    {
        pc = getInstance();
        cs = getContestService();
        Problem p = new Problem(vo);
        try
        {
            p.setContest(cs.findObject(vo.getContestCode()));
            pc.edit(p);
        } catch (Exception e)
        {
            if(e instanceof NonexistentEntityException)
            {
                throw new NonExistentObjectException();
            }
            throw new DatabaseException("ProblemService.update Operation");
        }
    }

    public void updateObject(Problem p)throws Exception
    {
        pc = getInstance();
        try
        {
            pc.edit(p);
        } catch (Exception e)
        {
            if(e instanceof NonexistentEntityException)
            {
                throw new NonExistentObjectException();
            }
            throw new DatabaseException("ProblemService.update Operation");
        }
    }

    public void delete(VOProblem vo)throws Exception
    {
        pc = getInstance();
        try
        {
            pc.destroy(vo.getIdProblem());
        } catch (Exception e)
        {
            if( e instanceof NonexistentEntityException)
            {
                throw new NonExistentObjectException();
            }
            throw new DatabaseException("ProblemService.delete Operation");
        }
    }

    public ArrayList<VOProblem> findall() throws Exception
    {
        pc = getInstance();
        try
        {
            ArrayList<VOProblem> ret = new ArrayList<VOProblem>();
            List<Problem> l = pc.findProblemEntities();
            for( Problem u : l)
                ret.add(u.getVO());
            return ret;
        }
        catch (Exception e)
        {
            if(e instanceof NonexistentEntityException)
                throw new NonExistentObjectException();
            throw new DatabaseException();
        }
    }




}
